package com.shuziren.controller.interciew_transcripts;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuziren.abnormal.CustomException;
import com.shuziren.core.vo.Result;
import com.shuziren.mapper.CandidateMapper;
import com.shuziren.mapper.InterviewPositionMapper;
import com.shuziren.pojo.entity.InterviewRecord;
import com.shuziren.pojo.dto.InterviewRecordDto;
import com.shuziren.pojo.vo.InterviewRecordVo;
import com.shuziren.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/interview-record")
public class InterviewRecordController {

    @Autowired
    private InterviewRecordService interviewRecordService;
    @Autowired
    private InterviewScheduleService interviewScheduleService;
    @Autowired
    private CandidateService candidateService;
    @Autowired
    private InterviewPositionService interviewPositionService;
    @Autowired
    private InterPersonService interPersonService;

    /**
     * 查询单个记录接口
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public Result getInterviewRecordById(Integer id) throws CustomException {
        InterviewRecordVo record = interviewRecordService.getInterviewRecordById(id);
        if (record == null) {
            return Result.fail("查询的面试记录不存在。");
        }
        return Result.success(record);
    }

    /**
     * 新增记录接口
     * @param record
     * @return
     */
    @PostMapping("/insert")
    public Result saveInterviewRecord(@Validated @RequestBody InterviewRecord record) throws CustomException {
        if (interviewScheduleService.getById(record.getScheduleId()) == null){
            throw new CustomException("该面试安排不存在");
        }
        if (candidateService.getById(record.getCandidateId()) == null){
            throw new CustomException("该候选人不存在");
        }
        if (interviewPositionService.getById(record.getPositionId()) == null){
            throw new CustomException("该职位不存在");
        }
        if (interPersonService.getById(record.getPersonId()) == null){
            throw new CustomException("该数字面试官不存在");
        }
        record.setUpdatedAt(new Date());
        boolean result = interviewRecordService.saveInterviewRecord(record);
        return Result.success(result);
    }

    /**
     * 更新记录接口
     * @param record
     * @return
     */
    @PostMapping("/update")
    public Result updateInterviewRecord(@Validated @RequestBody InterviewRecord record) throws CustomException {
        if (interviewScheduleService.getById(record.getScheduleId()) == null){
            throw new CustomException("该面试安排不存在");
        }
        if (candidateService.getById(record.getCandidateId()) == null){
            throw new CustomException("该候选人不存在");
        }
        if (interviewPositionService.getById(record.getPositionId()) == null){
            throw new CustomException("该职位不存在");
        }
        if (interPersonService.getById(record.getPersonId()) == null){
            throw new CustomException("该数字面试官不存在");
        }
        record.setUpdatedAt(new Date());
        boolean result = interviewRecordService.updateInterviewRecord(record);
        return Result.success(result);
    }

    /**
     * 删除记录接口
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteInterviewRecord(Integer id) throws CustomException {
        if (interviewRecordService.getInterviewRecordById(id) == null){
            throw new CustomException("该面试记录不存在");
        }
        boolean result = interviewRecordService.deleteInterviewRecord(id);
        return Result.success(result);
    }

    /**
     * 分页查询记录接口
     * @param queryVo
     * @return
     */
    @PostMapping("/page")
    public Result getAllInterviewRecordsPage(@Validated @RequestBody InterviewRecordDto queryVo) {
        IPage<InterviewRecordVo> page = interviewRecordService.getAllInterviewRecordsPage(queryVo);
        return Result.success(page);
    }
}
